package com.wdc.deliveryrobot.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import com.wdc.deliveryrobot.R;
import com.wdc.deliveryrobot.constant.Constants;
import com.wdc.deliveryrobot.databinding.DialogTaskRunnningBinding;

public class TaskRunningDialog extends Dialog{

    private Context mContext;
    private DialogTaskRunnningBinding binding;

    private TaskRunningListener taskRunningListener;

    private int time = 30;

    private Handler mHandler;

    public interface TaskRunningListener {
        void cancelTask();
        void keepRunningTask();
    }


    public TaskRunningDialog(@NonNull Context context, TaskRunningListener listener) {
        super(context);
        setCancelable(false);
        mContext = context;
        taskRunningListener = listener;
        binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.dialog_task_runnning, null, false);
        setContentView(binding.getRoot());
        initHandler();
        binding.bt1.setOnClickListener(v -> {
            listener.keepRunningTask();
            dismiss();

        });


        binding.bt3.setOnClickListener(v -> {
            listener.cancelTask();
            dismiss();
        });

        mHandler.sendEmptyMessage(1000);



    }

    private void initHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                switch (msg.what) {
                    case 1000:
                        if (time > 0) {
                            binding.tvTime.setText(time + "s");
                            time--;
                            mHandler.sendEmptyMessageDelayed(1000, 1000);
                        } else { // 倒计时结束 继续当前任务 对话框取消
                            if (Constants.TASK_NOW == null){
                                taskRunningListener.cancelTask();
                                dismiss();
                            }else {
                                taskRunningListener.keepRunningTask();
                                dismiss();
                            }

                        }
                        break;
                }
                return false;
            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        final int screenWidth = dm.widthPixels;
        final int screenHeight = dm.heightPixels;
        params.width = screenWidth / 5 * 3;
        params.height = screenHeight / 2;
        window.setAttributes(params);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
    }

    public void sendOneArrived(String point){
        //送物到达
        binding.tvContain.setText(getContext().getString(R.string.task_send_arrived,point));
        binding.tvTask.setText(getContext().getString(R.string.send_arrived));
    }

    public void sendArrived(String point){
        //送物完成
        Constants.TASK_NOW = null;
        binding.tvContain.setText(getContext().getString(R.string.task_send_arrived,point));
        binding.bt1.setVisibility(View.GONE);
        binding.tvTask.setText(getContext().getString(R.string.send_arrived));
        binding.bt3.setText("确定");
    }

    public void sendStop(){
        //任务暂停
        binding.tvContain.setVisibility(View.GONE);
        binding.tvTask.setText(getContext().getString(R.string.task_pause));
    }




}
